package IOpearation;

import Book.Book;
import Book.BookList;

import java.util.Arrays;
import java.util.Scanner;

/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-12-24
 * Time:10:47
 */
public class DelOpearation implements IOPearation{

    public static int findBook(BookList bookList){
        Scanner scanner = new Scanner(System.in);

        //输入信息
        System.out.println("请输入删除图书的名字");
        String name = scanner.nextLine();

        int size = bookList.getUsedSize();
        for (int i = 0; i < size ; i++) {
            // 判断这本书是否以及存在
            Book book1 = bookList.getBook(i);
            if (book1.getName().equals(name)){
                return i;
            }
        }
        return -1;
    }
    @Override
    public void work(BookList bookList) {
        System.out.println("删除书籍!");
        int choice = findBook(bookList);
        int size = bookList.getUsedSize();
        if (choice != -1){
            for (int i = choice; i <size - 1 ; i++) {
                bookList.setBooks(i,bookList.getBook(i+1));
            }
            System.out.println("删除成功!");
            bookList.setUsedSize(size - 1);
            return;
        }
        System.out.println("删除失败");
    }

}
